<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="园区编码" prop="parkCode">
        <el-input v-model="formData.parkCode" placeholder="请输入园区编码" />
      </el-form-item>
      <el-form-item label="园区名称" prop="parkName">
        <el-input v-model="formData.parkName" placeholder="请输入园区名称" />
      </el-form-item>
      <el-form-item label="所在省份" prop="province">
        <el-input v-model="formData.province" placeholder="请输入所在省份" />
      </el-form-item>
      <el-form-item label="所在城市" prop="city">
        <el-input v-model="formData.city" placeholder="请输入所在城市" />
      </el-form-item>
      <el-form-item label="所在区域" prop="region">
        <el-input v-model="formData.region" placeholder="请输入所在区域" />
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input v-model="formData.address" placeholder="请输入详细地址" />
      </el-form-item>
      <el-form-item label="所属公司编码" prop="companyCode">
        <el-input v-model="formData.companyCode" placeholder="请输入所属公司编码" />
      </el-form-item>
      <el-form-item label="园区面积" prop="parkArea">
        <el-input v-model="formData.parkArea" placeholder="请输入园区面积" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="启/停状态" prop="disabled">
        <el-radio-group v-model="formData.disabled">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="园区仓库面积" prop="parkWarehouseNum">
        <el-input v-model="formData.parkWarehouseNum" placeholder="请输入园区仓库面积" />
      </el-form-item>
      <el-form-item label="园区仓库数量" prop="parkWarehouseArea">
        <el-input v-model="formData.parkWarehouseArea" placeholder="请输入园区仓库数量" />
      </el-form-item>
      <el-form-item label="联系人" prop="linkMan">
        <el-input v-model="formData.linkMan" placeholder="请输入联系人" />
      </el-form-item>
      <el-form-item label="联系人电话" prop="linkPhone">
        <el-input v-model="formData.linkPhone" placeholder="请输入联系人电话" />
      </el-form-item>
      <el-form-item label="联系人邮箱" prop="linkEmail">
        <el-input v-model="formData.linkEmail" placeholder="请输入联系人邮箱" />
      </el-form-item>
      <el-form-item label="所属平台编码" prop="platformCode">
        <el-input v-model="formData.platformCode" placeholder="请输入所属平台编码" />
      </el-form-item>
      <el-form-item label="经度" prop="longitude">
        <el-input v-model="formData.longitude" placeholder="请输入经度" />
      </el-form-item>
      <el-form-item label="纬度" prop="latitude">
        <el-input v-model="formData.latitude" placeholder="请输入纬度" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { ParkApi, ParkVO } from '@/api/mdm/park'

/** 园区管理 表单 */
defineOptions({ name: 'ParkForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  parkCode: undefined,
  parkName: undefined,
  province: undefined,
  city: undefined,
  region: undefined,
  address: undefined,
  companyCode: undefined,
  parkArea: undefined,
  remark: undefined,
  disabled: undefined,
  parkWarehouseNum: undefined,
  parkWarehouseArea: undefined,
  linkMan: undefined,
  linkPhone: undefined,
  linkEmail: undefined,
  platformCode: undefined,
  longitude: undefined,
  latitude: undefined,
})
const formRules = reactive({
  parkCode: [{ required: true, message: '园区编码不能为空', trigger: 'blur' }],
  parkName: [{ required: true, message: '园区名称不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ParkApi.getPark(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ParkVO
    if (formType.value === 'create') {
      await ParkApi.createPark(data)
      message.success(t('common.createSuccess'))
    } else {
      await ParkApi.updatePark(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    parkCode: undefined,
    parkName: undefined,
    province: undefined,
    city: undefined,
    region: undefined,
    address: undefined,
    companyCode: undefined,
    parkArea: undefined,
    remark: undefined,
    disabled: undefined,
    parkWarehouseNum: undefined,
    parkWarehouseArea: undefined,
    linkMan: undefined,
    linkPhone: undefined,
    linkEmail: undefined,
    platformCode: undefined,
    longitude: undefined,
    latitude: undefined,
  }
  formRef.value?.resetFields()
}
</script>